<?php

namespace App\Models;

use App\Library\User\TemplateMessageAble;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;


/**
 * App\Models\User
 *
 * @property int $uid
 * @property int $gid
 * @property int $admingid 管理员ID
 * @property int $admincp 是否允许登录后台
 * @property string $username 用户名
 * @property string $password 密码
 * @property string $remember_token
 * @property string $api_token
 * @property string $email 邮箱
 * @property string $mobile 手机号
 * @property int $status 状态
 * @property int $newpm 新消息
 * @property int $emailstatus 邮箱验证状态
 * @property int $avatarstatus 头像验证状态
 * @property int $freeze 冻结账户
 * @property int $exp 经验值，积分
 * @property int $exp1
 * @property int $exp2
 * @property int $exp3
 * @property string $created_at
 * @property string $updated_at
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Address[] $addresses
 * @property-read \App\Models\UserConnect $connect
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserField[] $fields
 * @property-read \App\Models\UserGroup $group
 * @property-read \App\Models\UserInfo $info
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Item[] $items
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserLog[] $logs
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Material[] $materials
 * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Order[] $orders
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\PostItem[] $posts
 * @property-read \App\Models\Shop $shop
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Order[] $soldOrders
 * @property-read \App\Models\UserStat $stat
 * @property-read \App\Models\Wallet $wallet
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereAdmincp($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereAdminid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereApiToken($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereAvatarstatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereEmail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereEmailstatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereExp($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereExp1($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereExp2($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereExp3($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereFreeze($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereGid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereMobile($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereNewpm($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User wherePassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereRememberToken($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereUsername($value)
 * @mixin \Eloquent
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Order[] $boughts
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Cart[] $cartItems
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Collect[] $collects
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Order[] $solds
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereAdmingid($value)
 * @property int $puid 上级UID
 * @property int $level_id 用户等级id
 * @property string $jypassword 交易密码
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\BankCards[] $bankCards
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\CommissionRecords[] $commissionRecords
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\CommissionWithdraw[] $commissionWithdraws
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\IntegralRecords[] $integralRecords
 * @property-read \App\Models\Usermemberlevel $member_level
 * @property-read \App\Models\UserRelation $relation
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereJypassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereLevelId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User wherePuid($value)
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserBubbles[] $bubbles
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserPointRecord[] $pointRecords
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserTree[] $trees
 * @property int $licensestatus
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereLicensestatus($value)
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Achievements[] $Achievements
 */
class User extends Authenticatable
{
    use Notifiable, TemplateMessageAble;

    protected $table = 'user';
    protected $primaryKey = 'uid';
    protected $hidden = [
        'password', 'remember_token', 'api_token'
    ];
    protected $guarded = ['uid'];

    public $timestamps = false;

    /**
     *
     */
    public static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub

        static::creating(function (User $user){
            $user->created_at = time();
            $user->updated_at = time();
        });

        static::updating(function (User $user){
            $user->updated_at = time();
        });

        static::created(function (User $user){
            $user->info()->create();
            $user->stat()->create();
            $user->wallet()->create();
            //写入日志
            $user->logs()->create([
                'ip'=>request()->getClientIp(),
                'operate'=>'register',
                'created_at'=>time(),
                'src'=>request()->input('src', 'web')
            ]);
        });

        static::deleted(function (User $user){
            $user->info()->delete();
            $user->logs()->delete();
            $user->stat()->delete();
            $user->fields()->delete();
            $user->connect()->delete();
            $user->wallet()->delete();
        });
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function connect()
    {
        return $this->hasOne(UserConnect::class, 'uid', 'uid');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function fields()
    {
        return $this->hasMany(UserField::class, 'uid', 'uid');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function group()
    {
        return $this->belongsTo(UserGroup::class, 'gid', 'gid');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function member_level()
    {
        return $this->belongsTo(Usermemberlevel::class, 'level_id', 'level_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function info()
    {
        return $this->hasOne(UserInfo::class, 'uid', 'uid');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function logs()
    {
        return $this->hasMany(UserLog::class, 'uid', 'uid');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function stat()
    {
        return $this->hasOne(UserStat::class, 'uid', 'uid');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function wallet()
    {
        return $this->hasOne(Wallet::class, 'uid', 'uid');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function shop()
    {
        return $this->hasOne(Shop::class, 'uid', 'uid');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function items()
    {
        return $this->hasMany(Item::class, 'uid', 'uid');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function boughts()
    {
        return $this->hasMany(Order::class, 'buyer_uid', 'uid');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function solds()
    {
        return $this->hasMany(Order::class, 'seller_uid', 'uid');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function posts()
    {
        return $this->hasMany(PostItem::class, 'uid', 'uid');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function materials()
    {
        return $this->hasMany(Material::class, 'uid', 'uid');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function addresses()
    {
        return $this->hasMany(Address::class, 'uid', 'uid');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function cartItems(){
        return $this->hasMany(Cart::class, 'uid', 'uid');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function collects()
    {
        return $this->hasMany(Collect::class, 'uid', 'uid');
    }

    /**
     * 我的银行卡
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function bankCards()
    {
        return $this->hasMany(BankCards::class, 'uid', 'uid');
    }

    /**
     * 积分记录
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function integralRecords()
    {
        return $this->hasMany(IntegralRecords::class, 'uid', 'uid');
    }

    /**
     * 佣金记录
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function commissionRecords()
    {
        return $this->hasMany(CommissionRecords::class, 'uid', 'uid');
    }

    public function commissionWithdraws()
    {
        return $this->hasMany(CommissionWithdraw::class, 'uid', 'uid');
    }

    /**
     * 积分记录
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function pointRecords()
    {
        return $this->hasMany(UserPointRecord::class, 'uid', 'uid');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function relation()
    {
        return $this->hasOne(UserRelation::class, 'uid', 'uid');
    }

    public function trees()
    {
        return $this->hasMany(UserTree::class, 'uid', 'uid');
    }

    public function bubbles()
    {
        return $this->hasMany(UserBubbles::class, 'uid', 'uid');
    }

    public function Achievements()
    {
        return $this->hasMany(Achievements::class, 'uid', 'uid');
    }

    public function upuser()
    {
        $relation = $this->relation()->firstOrCreate([]);
        return $relation->upuser;
    }

    public function subusers()
    {
        return UserRelation::with('user')->where('upuid', $this->uid)->get()->map(function ($relation){
            return $relation->user;
        });
    }
}
